﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace NutriStat
{
    public partial class MySQLConnectDialog : NutriDialog
    {
        private OpenTableDialog m_fOpenTable;

        public MySQLConnectDialog()
        {
            InitializeComponent();
        }

        public MySQLConnectDialog(OpenTableDialog openTable)
        {
            InitializeComponent();
            m_fOpenTable = openTable;
        }

        ~MySQLConnectDialog()
        {
            m_fOpenTable.UseWaitCursor = false;
            m_fOpenTable.Enabled = true;
        }

        private void MySQLConnectDialog_Load(object sender, EventArgs e)
        {
            m_fOpenTable.Enabled = false;
            m_fOpenTable.UseWaitCursor = true;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            m_fOpenTable.Enabled = true;
            m_fOpenTable.UseWaitCursor = false;
            this.Close();
        }

        private void MySQLConnectDialog_FormClosed(object sender, FormClosedEventArgs e)
        {
            m_fOpenTable.UseWaitCursor = false;
            m_fOpenTable.Enabled = true;
        }

        private void buttonTest_Click(object sender, EventArgs e)
        {
            try
            {
                string server = textBoxServerName.Text;
                string username = textBoxUsername.Text;
                string password = textBoxPassword.Text;
                MessageBox.Show(password);
                MySqlConnection conn = new MySqlConnection("Server=" + server + ";Uid=" + username + ";Pwd=" + password + ";");
                conn.Open();
                conn.Close();
                conn.Dispose();
                MessageBox.Show(SharedStrings.NOTICE_CONNECTION_OK);
            }
            catch
            {
                MessageBox.Show(SharedStrings.NOTICE_CONNECTION_FAILED);
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                string server = textBoxServerName.Text;
                string database = textBoxDatabaseName.Text;
                string username = textBoxUsername.Text;
                string password = textBoxPassword.Text;
                MySqlConnection conn = new MySqlConnection("Server=" + server + ";Database=" + database + ";Uid=" + username + ";Pwd=" + password + ";");
                conn.Open();
                conn.Close();
                conn.Dispose();
                m_fOpenTable.ClearList();
                m_fOpenTable.ConnectionString = "Server=" + server + ";Database=" + database + ";Uid=" + username + ";Pwd=" + password + ";";
                m_fOpenTable.PopulateSQL();
                m_fOpenTable.UseWaitCursor = false;
                m_fOpenTable.Enabled = true;
                this.Close();
            }
            catch
            {
                MessageBox.Show(SharedStrings.NOTICE_CONNECTION_FAILED);
            }
        }

        /// <summary>
        /// Configures the form controls based on the user's preferences and regional settings.
        /// </summary>
        protected internal override void ConfigureFormControls()
        {
            this.Text = SharedStrings.CONNECT_MYSQL;
            //labelHeading.Text = SharedStrings.CONNECT_TO_A_SQL_DATABASE;
            labelServerName.Text = SharedStrings.SERVER_NAME;
            labelDatabaseName.Text = SharedStrings.DATABASE_NAME;
            //groupBoxAuthentication.Text = SharedStrings.AUTHENTICATION;
            labelUsername.Text = SharedStrings.USERNAME;
            labelPassword.Text = SharedStrings.PASSWORD;
            buttonOK.Text = SharedStrings.OK;
            buttonTest.Text = SharedStrings.TEST;
            buttonCancel.Text = SharedStrings.CANCEL;
        }
    }
}
